package testing;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import optimalServerSpaceCalculation.StartingSourceFragHelper;

public class optimalServerCalculation {

	public static void main(String[] args) {

		int startingSrcFrgNum = 90;
		int endingSrcFrgNum = 130;
		TreeMap<Integer, Double> frgSpace = new TreeMap<Integer, Double>();
		TreeMap<Integer, Integer> frgServer = new TreeMap<Integer, Integer>();

		/**
		 * wastage is constant at 250 bytes
		 */
		int startingWastage = 100;
		int endingWastage = 2000;

		TreeMap<Integer, Double> wstSpace = new TreeMap<Integer, Double>();
		TreeMap<Integer, Integer> wstServer = new TreeMap<Integer, Integer>();
		WastageSpaceHelper waste = new WastageSpaceHelper();

		for (int j = startingWastage; j <= endingWastage; j += 10) {
			// System.out.println(j);
			wstSpace.put(j, waste.calculateCumSpace(j));
			wstServer.put(j, waste.calculateMaxServer());
		}

		Set<Integer> keyw = wstSpace.keySet();
		Iterator<Integer> iterw = keyw.iterator();
		while (iterw.hasNext()) {
			int k = iterw.next();
			// System.out.println(k + ":" + wstServer.get(k) +
			// ":"+wstSpace.get(k));
		}

		StartingSourceFragHelper startingSrcHelper = new StartingSourceFragHelper();

		for (int i = startingSrcFrgNum; i <= endingSrcFrgNum; i++) {
//			System.out.println(i);
			frgSpace.put(i, startingSrcHelper.calculateCumSpace(i));
			frgServer.put(i, startingSrcHelper.calculateMaxServer());
		}
		Set<Integer> keys = frgSpace.keySet();
		Iterator<Integer> iter = keys.iterator();
		while (iter.hasNext()) {
			int key = (Integer) iter.next();
//			System.out.println(key + ":" + frgServer.get(key));

		}

		int N = 200; // number of servers user has in his cloud.
		int index = 0;
		// obtain the index position until where you need to operate
		Map<Integer, Integer> tempStarting = new HashMap<Integer, Integer>();
		Map<Double, Integer> tempWastage = new HashMap<Double, Integer>();
		TreeMap<Integer, Double> selectionList = new TreeMap<Integer, Double>();

		for (int i = startingSrcFrgNum; i < endingSrcFrgNum; i++) {
			if (frgServer.get(i) > N ) {
				System.out.println(frgServer.get(i));
				break;
			} else {
				int j = startingWastage;
				for (j = startingWastage; j < endingWastage; j += 10) {
					if (wstSpace.get(j) < frgSpace.get(i)) {
						// tempWstServer.put(j, wstServer.get(j));
					} else {
						break;
					}
				}
				System.out.println(wstSpace.get(j) + ":" + frgSpace.get(i)
						+ ":" + wstServer.get(j));
				// TreeMap<Integer, Integer> temp = new TreeMap<Integer,
				// Integer>(
				// tempWstServer);
				// System.out.println(temp.firstEntry());
				if (wstServer.get(j) < N) {
					selectionList.put(wstServer.get(j), wstSpace.get(j));
					tempStarting.put(wstServer.get(j), i);
					tempWastage.put(wstSpace.get(j),j);
				}
				// temp=null;
				// insert wstSpace and server number from
				// tempWstServer.temp.firstEntry into another map.
				// at the end of all the iterations sort this new TreeMap, find
				// the starting number of source fragments and the wastage
				// corresponding to best possible scenario
			} // end of else
		}// end of for

		// TreeMap<Integer,Double>treeSelectionList = new TreeMap<Integer,
		// Double>(selectionList);
         int numOfServer=selectionList.firstKey();
         double spaceOccupied=selectionList.get(numOfServer);
		System.out.println(selectionList.firstEntry()+"starting number of source frag"+tempStarting.get(numOfServer)+"wastage"+tempWastage.get(spaceOccupied));

	}

}
